<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/3/28
 * Time: 上午8:55
 * 余额变动操作
 */

namespace app\common\controller;
use app\common\model\CardMember;
use app\common\model\CardRank;
use app\common\model\CardUserGet;
use app\common\model\Paylog;
use think\Controller;
use Wechat\Wxobjopen;

class CardLevel extends Wxoption
{



    //检查成长  所属等级
    /*
     * card_id
     * user:code bonus
     * */
    public function checklevel($orderid){
        $Paylog = new Paylog();
        $payinfo = $Paylog->where('orderid',$orderid)->find();
        if ($payinfo['pay_fs'] == 0 || $payinfo['pay_fs'] == 2) {
            //查询领卡记录
            $map['have'] = 1;
            $map['fire'] = 1;
            $map['card_type'] = 'MEMBER_CARD';
            $map['openid'] = $payinfo['openid'];
            $CardUserGet = new CardUserGet();
            $user = $CardUserGet->where($map)->field('card_id,code,id,top_mid,openid,nickname,growup')->find();
            if ($user) {
                $price = intval($payinfo['price']);
                //更新成长值
                $CardUserGet->where(['id'=>$user['id']])->setInc('growup',$price);
                $growup = $user['growup'] + $price;
                $CardMember = new CardMember();
                $cardinfo = $CardMember->where(['card_id' => $user['card_id']])->field('is_rank,custom_field3')->find();
                $field3 = json_decode($cardinfo['custom_field3'], true);
                //是否开启等级
                if ($field3['name'] == '等级' && $cardinfo['is_rank'] == '1') {
                    $CardRank = new CardRank();
                    //获取等级列表
                    $rank = $CardRank->where(['card_id' => $user['card_id']])->find();
                    if ($rank) {
                        //符合的等级信息
                        $rank_id = $this->getrankid($user['card_id'], $growup);
                        $rankinfo = $CardRank->where(['id' => $rank_id])->field('id,live,is_drop,live_title,bg_img_url')->find();
                        //检查该等级是否可变
                        $datas['card_id'] = $user['card_id'];
                        $datas['code'] = $user['code'];
                        $datas['level'] = $rankinfo['live'];
                        $datas['is_drop'] = $rankinfo['is_drop'];
                        $checkuserrank = $this->checkuserrank($datas);
                        if ($checkuserrank == 1) {
                            $maps['card_id'] = array('eq', $user['card_id']);
                            $data['live_title'] = $rankinfo['live_title'];
                            $data['code'] = $user['code'];
                            $data['card_id'] = $user['card_id'];
                            $data['background_pic_url'] = $rankinfo['bg_img_url'];
                            $res['wx'] = $this->updatelevel($data);         //等级显示
                            $data['live'] = $rankinfo['live'];
                            $res['os'] = $this->updateuserrank($data);      //等级数据
                        } else {
                            $res = '无等级改变';
                        }
                    } else {
                        $res = '无等级设置';
                    }
                } else {
                    $data['level'] = '查看';
                    $data['code'] = $user['code'];
                    $data['card_id'] = $user['card_id'];
                    $res = $this->updatelevel($data);
                }
            }else{
                $res = '无领卡';
            }
        }else{
            $res = '交易方式不符合';
        }
//        return $res;
    }

    //获取会员等级值
    public function getrankid($card_id, $growup)
    {
        $map['card_id'] = $card_id;
        $CardRank = new CardRank();
        $res = $CardRank->where($map)->order('growup desc')->field('id,growup,live_title,live')->select();
        foreach ($res as $k => $v) {
            if ($growup >= $v['growup']) {
                $sdata[] = $v['id'];
            }
        }
        if (!isset($sdata[0])) {
            $sdata[0] = 0;
        }
        if (isset($sdata)) {
            $level = $sdata[0];
        } else {
            $level = 0;
        }
        return $level;
    }

    //积分更新微信操作
    //card_id  code level_title
    public function updatelevel($arr)
    {
        try {
            //实例化微信
            $options = $this->options($arr['card_id']);
            $WxObj = new Wxobjopen($options);
            $data = [
                'code' => $arr['code'],
                'card_id' => $arr['card_id'],
                'custom_field_value3' => $arr['level_title'],
                'notify_optional' => [
                    'is_notify_bonus' => false,
                    'is_notify_balance' => false,
                    'is_notify_custom_field3' => true,
                ],
            ];
            if (!empty($arr['background_pic_url'])) {
                $data['background_pic_url'] = $arr['background_pic_url'];
            }
            $res = $WxObj->updateUserInfo($data);
        } catch (\Exception $e) {
            $res = ['errcode' => 9999, 'message' => $e->getMessage()];
        }
        return $res;
    }

    //检查该等级是否可变
    /*
     * card_id   code  level  is_drop
     */
    public function checkuserrank($arr){
        try{
//            $CardRank = new CardRank();
            $CardUserGet = new CardUserGet();
            $map['card_id'] = $arr['card_id'];
            $map['code'] = $arr['code'];
            $user = $CardUserGet->where($map)->field('rank,rank_lock')->find();
//            $maps = ['card_id'=>$arr['card_id'],'live'=>$user['rank']];
//            $rank = $CardRank->where($maps)->value('is_drop');
            //当前等级是否大于改变等级
            if ($user['rank'] == $arr['level']) {
                $res = 2;
            }else{
                //当前等级是否需要降级   或   当前等级是否能改变  （等级不可降，1 / 2   改变 / 不变）
                if ($user['rank'] > $arr['level'] || $arr['is_drop'] == 2) {
                    $res = 2;
                } else {
                    //可变
                    $res = 1;
                }
            }
        }catch (\Exception $e) {
            $res = ['errcode' => 9999, 'message' => $e->getMessage()];
            $res = 1;
        }
        return $res;

    }

    //card_id code    live  live_title
    public function updateuserrank($arr){
        try{
            $CardRank = new CardRank();
            $maps['id'] = $arr['rank_id'];
            $rank = $CardRank->where($maps)->find();
            $CardUserGet = new CardUserGet();
            $map['card_id'] = $arr['card_id'];
            $map['code'] = $arr['code'];
            $data['rank'] = $rank['live'];
            $data['rank_name'] = $rank['live_title'];
            $res = $CardUserGet->save($data,$map);
        }catch (\Exception $e) {
            $res = ['errcode' => 9999, 'message' => $e->getMessage()];
        }
        return $res;
    }

    
}